package com.gxdf.extendedcontrols.gxdfclient.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

public class DatePickerFragment extends DialogFragment implements
		OnDateSetListener {
	
	int year;
	int month;
	int dayDate;
	OnDateSetListener mDateSetListener;

	public DatePickerFragment(OnDateSetListener callback, String dateString) {
		mDateSetListener = (OnDateSetListener) callback;
		parseDateString(dateString);
		
	}
	

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
	//	text.setText(String.valueOf(year) + String.valueOf(monthOfYear) + String.valueOf(dayOfMonth) );
		
	 	this.year= year;
	 	this.month= monthOfYear;
	 	this.dayDate = dayOfMonth;

	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		//final Calendar c = Calendar.getInstance(); 
		//int year = c.get(Calendar.YEAR); 
		//int month = c.get(Calendar.MONTH); 
		//int day = c.get(Calendar.DAY_OF_MONTH); 		
		
		int year = getYear();
		int month = getMonth();
		int day = getDayDate();
		
		
		return new DatePickerDialog(getActivity(),mDateSetListener,year, month, day);
	}
	
	public String getDateToString(){		
		return String.valueOf(this.year) + String.valueOf(this.month) + String.valueOf(this.dayDate);
	}
	
	
	private void parseDateString(String dateString) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
		try {
			//Date date = sdf.parse(dateString);
			
			Calendar calendar =  Calendar.getInstance();
			calendar.setTime(sdf.parse(dateString));
						
			setYear(calendar.get(Calendar.YEAR));
			setMonth(calendar.get(Calendar.MONTH));
			setDayDate(calendar.get(Calendar.DAY_OF_MONTH));
			
			
			
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}

	public int getDayDate() {
		return dayDate;
	}

	public void setDayDate(int dayDate) {
		this.dayDate = dayDate;
	}
	
	

}
